//
//  MyTestViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/16.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MyTestViewController.h"
#import "AppConfig.h"
#import "MyTestCell.h"
#import "UIViewController+NavItem.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "WaitPayTestViewController.h"
#import "WaitForTestViewController.h"
#import "TestResultViewController.h"
#import "UIViewController+Create.h"
#import "MySelfViewController.h"
#import "HomeViewController.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MyTestModel.h"
#import "TestResultViewController.h"

@interface MyTestViewController ()<UITableViewDelegate , UITableViewDataSource,
                                    QXNetWorkToolDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong)NSMutableArray *data;

@property (strong, nonatomic) MyTestCell* myCell;

@end

@implementation MyTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.data = [[NSMutableArray alloc] init];
}

- (void)config
{
    UserDataSingleton *userData = [UserDataSingleton shareInstance];
    
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
    
    tool.QXNetWorkDelegate = self;
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:ExamInfoListInterface andParameter:@[@{@"user_id":userData.user_id}] tag:1];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:mes toView:self.view];

            break;
    }
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    if (Response == nil)
    {
        return;
    }
    switch (tag)
    {
        case 1:
        {
            [self.data removeAllObjects];
            for (NSDictionary *dic in Response)
            {
                MyTestModel *model = [MyTestModel MyTestWithDict:dic];
                [self.data addObject:model];
            }
            [self.myTableView reloadData];
        }
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    
    [self config];
}

-(void) configNavigationItem
{
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"我的考试";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    [self configNavigationBackAction:^{
        
        if (self.isReturnTOShouYe)
        {
            UITabBarController * vc = [self.navigationController.viewControllers firstObject];
            
            [vc setSelectedIndex:0];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else
        {     
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];

}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.data count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.myCell)
    {
        return [self.myCell getHeight];
    }
    return 120;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"MyTestTableViewCell";
    self.myCell = [self.myTableView dequeueReusableCellWithIdentifier:identifier];
    if(self.myCell == nil)
    {
        self.myCell = [MyTestCell loadFromNib];
    }
    [self configCell:indexPath.row cell:self.myCell];
    return self.myCell;
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MyTestModel *model = [self.data objectAtIndex:indexPath.row];
    
    if ([model.state_code isEqualToString:@"1"])    //已报名
    {
        WaitForTestViewController *obj = [WaitForTestViewController create];
        obj.exam_id = model.id;
        obj.title = @"等待应答";
        
        [self.navigationController pushViewController:obj animated:YES];
    }
    else if ([model.state_code isEqualToString:@"2"])   //审核通过(报名通过)
    {
        WaitPayTestViewController *obj = [WaitPayTestViewController create];
        obj.exam_id = model.id;

        [self.navigationController pushViewController:obj animated:YES];
    }
    if ([model.state_code isEqualToString:@"3"])    //等待考试
    {
        WaitForTestViewController *obj = [WaitForTestViewController create];
        obj.exam_id = model.id;
        obj.title = @"等待考试";
        
        [self.navigationController pushViewController:obj animated:YES];
    }
    else if ([model.state_code isEqualToString:@"4"]
        || [model.state_code isEqualToString:@"5"]) //考试已通过、考试未通过
    {
        TestResultViewController *obj = [TestResultViewController create];
        obj.exam_id = model.id;
        
        [self.navigationController pushViewController:obj animated:YES];
    }    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)configCell :(NSInteger)row cell:(MyTestCell *) mycell
{
    mycell.leftBt.tag = row;
//    [mycell.leftBt addTarget:self action:@selector(pressEnterBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    MyTestModel *model = [self.data objectAtIndex:row];
    
    NSMutableString *time = [NSMutableString stringWithString:model.exam_time];
    [time replaceCharactersInRange:NSMakeRange(4, 1) withString:@"年"];
    [time replaceCharactersInRange:NSMakeRange(7, 1) withString:@"月"];
    [time replaceCharactersInRange:NSMakeRange(10, 1) withString:@"日 "];
//    [time replaceCharactersInRange:NSMakeRange(17, 3) withString:@""];
    
    if ([model.major_name isEqualToString:@"音乐基础"])
    {
        mycell.titleImg.image = [UIImage imageNamed:@"img_myTest_musicBase"];
    }
    else if ([model.major_name isEqualToString:@"爵士鼓"])
    {
        mycell.titleImg.image = [UIImage imageNamed:@"Img_myTest_jueshigu"];
    }
    else if ([model.major_name isEqualToString:@"电吉他"])
    {
        mycell.titleImg.image = [UIImage imageNamed:@"img_myTest_dianjita"];
    }
    else if ([model.major_name isEqualToString:@"电贝司"])
    {
        mycell.titleImg.image = [UIImage imageNamed:@"img_myTest_dianbeisi"];
    }
    
    mycell.titleLabel.text = model.major_name;
    
    if ([mycell.titleLabel.text isEqualToString:@"音乐基础"])
    {
        mycell.titleLabel.text = @"音乐理论";
    }
    
    mycell.testAddrLabel.text = model.exam_site_name;
    mycell.testTimeLabel.text = time;
    [mycell.stausBtn setTitle:model.state_msg forState:UIControlStateNormal];
}

//点击进入
//-(void)pressEnterBtn : (UIButton *)sender
//{
//    [self.navigationController pushViewController:[WaitForTestViewController create] animated:YES];
//}


@end
















